<template>
  <div>
    <h3>食物测评</h3>
    <van-sticky>
      <van-nav-bar title="食物健康测评" right-text="按钮" />
      <van-search v-model="value" placeholder="搜索你今天的食物" />
    </van-sticky>
    <van-row>
      <van-col span="6">
        <van-sidebar v-model="activeKey">
          <van-sidebar-item title="主食" @click="getZhu" />
          <van-sidebar-item title="蛋奶豆制品" @click="getDou" />
          <van-sidebar-item title="蔬菜类" @click="getShu" />
          <van-sidebar-item title="水果类" />
          <van-sidebar-item title="肉类" />
          <van-sidebar-item title="酒水类" />
          <van-sidebar-item title="海鲜类" />
          <van-sidebar-item title="其他" />
        </van-sidebar>
      </van-col>
      <van-col span="18">
        <van-row>
          <van-col
            v-for="(item, index) in data"
            :key="index"
            span="8"
            @click="ddd(item)"
            class="divv"
            :class="{ frame: item.quality }"
          >
            <img :src="item.imgUrl" alt="" />
            <p>{{ item.name }}</p>
          </van-col>
        </van-row>
      </van-col>
    </van-row>
    <van-popup
      v-if="$store.state.stdata.length"
      class="vanDB"
      v-model="show"
      position="bottom"
      :overlay="false"
      :style="{ height: '20%' }"
    >
      <div class="ccc">
        <span>已选食物({{ num }})</span>
        <van-button type="danger" size="small" round @click="app"
          >去测评</van-button
        >
      </div>
      <div class="bbb">
        <van-tag
          class="biao"
          v-for="(item, index) in $store.state.stdata"
          :key="index"
          closeable
          size="medium"
          type="primary"
          @close="close(item)"
        >
          {{ item.name }}
        </van-tag>
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeKey: 0,
      value: "",
      data: [],
    };
  },
  mounted() {
    this.getZhu();
  },
  methods: {
    getDou() {
      this.$http("/豆奶制品.json", "get").then((res) => {
        this.data = res.data;
        this.light();
      });
    },
    getShu() {
      this.$http("/蔬菜类.json", "get").then((res) => {
        this.data = res.data;
        this.light();
      });
    },
    getZhu() {
      this.$http("/主食.json", "get").then((res) => {
        console.log(res.data);
        this.data = res.data;
        this.light();
      });
    },
    ddd(item) {
      this.$store.commit("ddd", item);
      this.light();
    },
    light() {
      this.data.forEach((t, i) => {
        this.data[i].quality = 0;
      });
      this.$store.state.stdata.forEach((item, index) => {
        this.data.forEach((t, i) => {
          if (t.name == item.name) {
            this.data[i].quality = 1;
          }
        });
      });
    },
    close(item) {
      this.$store.commit("close", item);
      this.light();
    },
    app() {
      this.$router.push("/assess");
    },
  },
  components: {},
  computed: {
    show() {
      if (this.$store.state.stdata.length == 0) {
        return false;
      } else {
        return true;
      }
    },
    num() {
      return this.$store.state.stdata.length;
    },
  },
  filter: {},
  watch: {},
  directives: {},
};
</script>

<style lang="scss">
.divv {
  height: 100px;
  text-align: center;
  img {
    width: 50px;
    height: 50px;
    border-radius: 5px;
  }
  p {
    font-size: 14px;
    font-weight: bold;
  }
}
.vanDB {
  border-top: 1px #000 solid;
}
.ccc {
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding: 2px 10px 0;
}
.bbb {
  box-sizing: border-box;
  padding: 10px;
}
.biao {
  margin-right: 5px;
  margin-bottom: 5px;
}
.frame {
  img {
    border: 3px rgb(234, 186, 245) solid;
  }
  p {
    color: rgb(216, 149, 240);
    font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
  }
}
</style>
